﻿using System;
using System.Drawing;
using System.Windows.Forms;

// SkypeScreenSaver, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace SkypeScreenSaver
{
    public partial class Form1 : Form
    {
        //
        CWakeUpDisk cwd = new CWakeUpDisk();
        CWakeUpSkype cws = new CWakeUpSkype();
        //
        public Form1()
        {
            InitializeComponent();
            //
            Cursor.Hide();
#if !DEBUG
            this.WindowState = FormWindowState.Maximized;
            MonitorOnOff.Set(this.Handle, false);
#endif
            //
            this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.Form1_Scroll_MouseWheel);
            //
            //Screen[] ss = Screen.AllScreens;
        }

        protected override void WndProc(ref Message m)
        {
            // Quit if receiving a WM_NCDESTROY message
            if (m.Msg == 0x0082)
            {
                this.Close();
            }
            base.WndProc(ref m);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //
            cwd.eventWakeUp += wakeUpMe;
            cws.eventWakeUp += wakeUpMe;
            //
            cwd.Start();
            cws.Start();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            // dwukrotne odpalanie eventu
            this.FormClosed -= new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
            //
            MonitorOnOff.Set(this.Handle, true);
            cwd.Stop();
            cws.Stop();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            this.Close();
        }

        private Point MouseXY;

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (!MouseXY.IsEmpty)
            {
                if (MouseXY != new Point(e.X, e.Y)) Close();
            }
            MouseXY = new Point(e.X, e.Y);
            //this.Close();
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (!MouseXY.IsEmpty)
            {
                if (MouseXY != new Point(e.X, e.Y)) Close();
                if (e.Clicks > 0) Close();
            }
            MouseXY = new Point(e.X, e.Y);
        }

        private void Form1_Scroll_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            this.Close();
        }

        private void wakeUpMe()
        {
            if (this.InvokeRequired)
            {
                dMonitorOn d = new dMonitorOn(monitorOn);
                this.Invoke(d, new object[] { this });
            }
            else
            {
                monitorOn(this);
            }
        }

        delegate void dMonitorOn(Form f);
        private void monitorOn(Form f)
        {
            MonitorOnOff.Set(f.Handle, true);//nie zaszkodzi
            f.Close();
        }

    }
}
